package net.yunxinyong.cec.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.yunxinyong.cec.entity.Cec702Entity;
import net.yunxinyong.cec.entity.Cec702EntityExample;
import net.yunxinyong.cec.mapper.Cec702EntityMapper;
import net.yunxinyong.cec.service.Cec702Service;
import net.yunxinyong.cec.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class Cec702ServiceImpl implements Cec702Service {
    @Autowired
    private Cec702EntityMapper cec702EntityMapper;

    @Override
    public PageBean findAll(int page, int rows, Cec702Entity cec702) {
        Cec702EntityExample cec711OneEntityExample = new Cec702EntityExample();
        Cec702EntityExample.Criteria criteria = cec711OneEntityExample.createCriteria();
        if (cec702.getId() != null) {
            criteria.andIdEqualTo(cec702.getId());
        }
        if (page != 0 && rows != 0) {
            PageHelper.startPage(page, rows);
        }
        List<Cec702Entity> list = cec702EntityMapper.selectByExample(cec711OneEntityExample);
        //设置分页信息
        PageBean<Cec702Entity> pageBean = new PageBean<>();
        PageInfo<Cec702Entity> pageInfo = new PageInfo<>(list);
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setPageDatas(list);
        return pageBean;
    }

    @Override
    public List<Cec702Entity> findOneByBaseId(int baseid) {
        Cec702EntityExample cec702EntityExample = new Cec702EntityExample();
        Cec702EntityExample.Criteria criteria = cec702EntityExample.createCriteria();
        criteria.andBaseIdEqualTo(baseid);
        List<Cec702Entity> list = cec702EntityMapper.selectByExample(cec702EntityExample);
        return list;
    }

    @Override
    public int updateById(int id, int status) {
        Cec702Entity cec702Entity = new Cec702Entity();
        cec702Entity.setId(id);
        cec702Entity.setStatus(status);
        cec702Entity.setUpdateTime(new Date());
        int count = cec702EntityMapper.updateByPrimaryKeySelective(cec702Entity);
        return count;
    }

    @Override
    public int deleteById(int id) {
        Cec702Entity cec702Entity = new Cec702Entity();
        cec702Entity.setId(id);
        cec702Entity.setStatus(1);
        int count = cec702EntityMapper.updateByPrimaryKeySelective(cec702Entity);
        return count;
    }

    @Override
    public int insertOne(int baseid) {
        Cec702Entity cec702Entity = new Cec702Entity();
        cec702Entity.setBaseId(baseid);
        //更改表时间
        cec702Entity.setUpdateTime(new Date());
        //创建表时间
        cec702Entity.setCreateTime(new Date());
        int count = cec702EntityMapper.insertSelective(cec702Entity);
        return count;
    }


}
